import {
	productionMaterialCreate,
	productionMaterialGet,
	productionMaterialUpdate, productionMaterialUpdateAuditState
} from '../../../../../../api/production/collectMaterial';
import handlerCache from '../../../../../../mixins/handler-cache';
import remoteSearchBatchCodeInput from '../../../../../production/qhPrd/productConfigCotton/components/remote-search-batch-code-input';
import { formatDate } from '../../../../../../utils/jk/jk-tool';
import jkSaveButton from '../../../../../jk-components/jk-save-button';
import jkUpdateButton from '../../../../../jk-components/jk-update-button';
import remoteSearchEmpInput from '../../../../../technology/order/components/remote-search-emp-input';
import remoteSearchProductInput from '../../../../../technology/order/components/remote-search-product-input';
import checkboxProductDialog from '../../../../order/components/checkbox-product-dialog';
let _this = null;
export default {
	components: { checkboxProductDialog, jkSaveButton, jkUpdateButton, remoteSearchProductInput, remoteSearchEmpInput, remoteSearchBatchCodeInput },
	mixins: [handlerCache],
	props: {
		pageType: {
			type: String,
			default: 'add'
		}
	},
	data() {
		return {
			prefix: 'spin:specSheet',
			apiItem: {
				'save': productionMaterialCreate,
				'update': productionMaterialUpdate
			},
			dataId: null,
			selectProductDialogState: false,
			tableLoading: false,
			updateAuditLoading: false,
			auditStateLoading: false,
			saveBtnLoading: false,
			workshopList: [],
			showLoading: true,
			saveDialogState: false,
			ruleForm: {
				'code': '编号自动生成',
				'batchCode': '',
				'batchId': 0,
				'billDate': '',
				'empCode': '',
				'empId': 0,
				'empName': '',
				'productCode': '',
				'productId': 0,
				'productModels': '',
				'productName': '',
				'productOrderPlanCode': '',
				'productOrderPlanId': 0,
				'remark': '',
				'totalQty': 0,
				'totalWeight': 0,
				'unitCode': '',
				'unitId': 0,
				'unitName': '',
				'workshopCode': '',
				'workshopId': 0,
				'workshopName': '',
				'productList': []
			},
			rules: {
				workshopId: [{ required: true, trigger: 'change' }],
				billDate: [{ required: true, trigger: 'change' }],
				empId: [{ required: true, trigger: 'change' }],
				productId: [{ required: true, trigger: 'change' }],
				batchCode: [{ required: true, trigger: 'change' }]
			},
			saveAndEnableBtnLoading: false
		};
	},
	methods: {
		onVisibleChangeOfSelectProductDialog(e) {
			this.selectProductDialogState = e;
		},
		onConfirmBtnOfSelectProductDialog(list) {
			if (list.length) {
				this.selectProductDialogState = false;
				list.forEach(x => {
					this.ruleForm.productList.push({
						productId: x.id,
						productCode: x.code,
						productName: x.name,
						productModels: x.models,
						unitId: x.unitId,
						unitCode: x.unitCode,
						unitName: x.unitName,
						batchCode: '',
						batchId: null,
						subQty: 10,
						subWeight: 10
					});
				});
			}
		},
		onSelectFrontSpinPlanChange(e) {
			if (Object.keys(e).length) {
				this.$set(this.ruleForm, 'productOrderPlanId', e.id);
				this.$set(this.ruleForm, 'productOrderPlanCode', e.code);
				this.$set(this.ruleForm, 'productOrderId', e.productOrderId);
				this.$set(this.ruleForm, 'productOrderCode', e.productOrderCode);
				this.$set(this.ruleForm, 'productId', e.coarseSandId);
				this.$set(this.ruleForm, 'productCode', e.coarseSandCode);
				this.$set(this.ruleForm, 'productName', e.coarseSandName);
				this.$set(this.ruleForm, 'productModels', e.coarseSandModels);
				this.$set(this.ruleForm, 'batchId', e.batchId);
				this.$set(this.ruleForm, 'batchCode', e.batchCode);
				this.$set(this.ruleForm, 'productionQty', e.productionQty);
			}
		},
		onEditBtn() {
			this.$toOtherRouter({
				name: 'editTechnology',
				query: {
					id: this.$route.query.id ? Number(this.$route.query.id) : null
				}
			});
		},
		async getDependentData() {
			this.showLoading = true;
			this.dataId = this.$route.query.id ? Number(this.$route.query.id) : null;
			this.$nextTick(() => {
				this.$refs.ruleForm.clearValidate();
			});
			// 新增页
			this.workshopList = this.$store.state.user.userWorkshop.workshopList;
			if (this.pageType === 'add') {
				this.$set(this.ruleForm, 'empId', this.$store.state.user.empInfo.id);
				this.$set(this.ruleForm, 'empCode', this.$store.state.user.empInfo.code);
				this.$set(this.ruleForm, 'empName', this.$store.state.user.empInfo.name);
				this.workshopList = this.$store.state.user.userWorkshop.workshopList;
				this.$set(this.ruleForm, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
				this.$set(this.ruleForm, 'workshopCode', this.$store.state.user.userWorkshop.defaultWorkshopCode);
				this.$set(this.ruleForm, 'workshopName', this.$store.state.user.userWorkshop.defaultWorkshopName);
				this.$set(this.ruleForm, 'billDate', formatDate(Date.now()));
			}
			if (this.dataId) await this.getDetailRequest();
			this.showLoading = false;
		},
		onSelectBatchCodeChange(e) {
			if (Object.keys(e).length) {
				this.$set(this.ruleForm, 'batchCode', e.batchCode);
			}
		},
		onSelectEmpChange(e) {
			if (Object.keys(e).length) {
				this.$set(this.ruleForm, 'empId', e.id);
				this.$set(this.ruleForm, 'empCode', e.code);
				this.$set(this.ruleForm, 'empName', e.name);
			}
		},
		onSelectProductChange(e) {
			if (Object.keys(e).length) {
				this.setProductDependentData(e);
			}
		},
		setProductDependentData(e) {
			this.$set(this.ruleForm, 'batchCodeId', null);
			this.$set(this.ruleForm, 'batchCode', '');
			this.$set(this.ruleForm, 'productId', e.id);
			this.$set(this.ruleForm, 'productCode', e.code);
			this.$set(this.ruleForm, 'productName', e.name);
			this.$set(this.ruleForm, 'productModels', e.models);
		},
		defaultRowData() {
			return {
				processId: null,
				processCode: '',
				processName: ''
			};
		},
		getParamData() {
			const params = JSON.parse(JSON.stringify(this.ruleForm));
			return params;
		},
		onConfirmBtn(type) {
			this.$refs['ruleForm'].validate(valid => {
				if (valid) {
					this.saveRequest(type);
				} else {
					this.$tipsBar(this, 'validator');
				}
			});
		},
		onWorkshopChange() {},
		saveRequest(type) {
			if (type === 2) {
				this.saveAndEnableBtnLoading = true;
			} else {
				this.saveBtnLoading = true;
			}
			// 新增
			if (!this.dataId) {
				this.ruleForm.id = null;
				this.apiItem.save(this.getParamData()).then(res => {
					if (res.data.status === 200) {
						this.dataId = res.data.res;
						if (type === 2) {
							this.updateState(this.dataId).then(res => {
								this.saveAndEnableBtnLoading = false;
								if (res.data.status === 200) {
									this.$tipsBar(this, 'save');
									this.onJump(this.dataId);
								} else {
									this.$tipsBar(this, 'save');
									this.onJump(this.dataId);
								}
							});
						} else {
							this.saveBtnLoading = false;
							this.$tipsBar(this, 'save');
							this.onJump(this.dataId);
						}
					} else {
						this.saveBtnLoading = false;
						this.saveAndEnableBtnLoading = false;
					}
				});
				return;
			}
			// 编辑
			this.apiItem.update(this.getParamData()).then(res => {
				if (res.data.status === 200) {
					if (type === 2) {
						this.updateState(this.dataId).then(res => {
							this.saveAndEnableBtnLoading = false;
							if (res.data.status === 200) {
								this.$tipsBar(this, 'save');
								this.getDependentData();
							} else {
								this.$tipsBar(this, 'save');
								this.getDependentData();
							}
						});
					} else {
						this.saveBtnLoading = false;
						this.$tipsBar(this, 'save');
						this.getDependentData();
					}
				} else {
					this.saveBtnLoading = false;
					this.saveAndEnableBtnLoading = false;
				}
			});
		},
		onAuditStateChange() {
			this.auditStateLoading = true;
			this.updateState(this.ruleForm.id).then(res => {
				this.auditStateLoading = false;
				if (res.data.status === 200) {
					this.getDependentData();
				}
			});
		},
		updateState(id) {
			return productionMaterialUpdateAuditState({ id });
		},
		onVisibleChange(e) {
			this.$emit('on-visible-change', e);
			if (!e) {
				Object.assign(this.$data, this.$options.data());
				this.$nextTick(() => {
					this.$refs.ruleForm.resetFields();
				});
			}
		},
		getDetailRequest() {
			return productionMaterialGet({ id: this.dataId }).then(res => {
				this.showLoading = false;
				if (res.data.status === 200) {
					const resData = JSON.parse(JSON.stringify(res.data.res));
					delete resData.specSheetParamList;
					this.ruleForm = resData;
				}
			});
		},
		onJump(id) {
			this.$store.dispatch('tagsView/delView', { path: this.$route.path });
			this.$toOtherRouter({
				name: 'editCollectMaterial',
				query: { id }
			});
		},
		onJumpList() {
			this.$store.dispatch('tagsView/delView', { path: this.$route.path });
			this.$toMainRouter({
				name: 'CollectMaterial'
			});
		},
		clearCacheMethod() {
			Object.assign(this.$data, this.$options.data());
			this.$nextTick(() => {
				this.$refs.ruleForm.clearValidate();
				this.getDependentData();
			});
		}
	},
	mounted() {
		_this = this;
	}
};
